package com.hm.jiaowuxitong.ui;

import com.hm.jiaowuxitong.service.CourseServiceImp;
import com.hm.jiaowuxitong.service.ICourseService;

import com.hm.jiaowuxitong.util.UIUtil;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.HashMap;
import java.util.Map;

public class AddCourseInfoUI {
    private int frame_width=770;
    private int frame_height=460;

    public AddCourseInfoUI(){
        //创建添加学生信息的面板
        JFrame frame=new JFrame("添加课程信息");
        //设置面板的宽高
        frame.setSize(frame_width,frame_height);
        //禁止面板放大缩小
        frame.setResizable(false);
        //创建画布
        JPanel panel=new JPanel();
        //创建方向布局
        SpringLayout layout=new SpringLayout();
        panel.setLayout(layout);
        //创建文本框控件
        JLabel label1=new JLabel("课程号:");
        layout.putConstraint(SpringLayout.WEST,label1,190,SpringLayout.WEST,panel);
        layout.putConstraint(SpringLayout.NORTH,label1,50,SpringLayout.NORTH,panel);
        label1.setForeground(Color.black);
        label1.setFont(new Font(" ", 3, 20));
        //创建文本编辑框控件
        JTextField jtf1=new JTextField(15);
        layout.putConstraint(SpringLayout.NORTH,jtf1,57,SpringLayout.NORTH,panel);
        layout.putConstraint(SpringLayout.WEST,jtf1,5,SpringLayout.EAST,label1);
        jtf1.setForeground(Color.black);
        jtf1.setFont(new Font(" ", 3, 15));
        //创建文本框控件
        JLabel label2=new JLabel("课程名:");
        layout.putConstraint(SpringLayout.WEST,label2,185,SpringLayout.WEST,panel);
        layout.putConstraint(SpringLayout.NORTH,label2,10,SpringLayout.SOUTH,label1);
        label2.setForeground(Color.black);
        label2.setFont(new Font(" ", 3, 20));
        //创建文本编辑框控件
        JTextField jtf2=new JTextField(15);
        layout.putConstraint(SpringLayout.WEST,jtf2,5,SpringLayout.EAST,label2);
        layout.putConstraint(SpringLayout.NORTH,jtf2,10,SpringLayout.SOUTH,jtf1);
        jtf2.setForeground(Color.black);
        jtf2.setFont(new Font(" ", 3, 15));
        //创建文本框控件
        JLabel label3=new JLabel("教师号:");
        layout.putConstraint(SpringLayout.WEST,label3,185,SpringLayout.WEST,panel);
        layout.putConstraint(SpringLayout.NORTH,label3,10,SpringLayout.SOUTH,label2);
        label3.setForeground(Color.black);
        label3.setFont(new Font(" ", 3, 20));
        //创建文本编辑框控件
        JTextField jtf3=new JTextField(15);
        layout.putConstraint(SpringLayout.WEST,jtf3,5,SpringLayout.EAST,label3);
        layout.putConstraint(SpringLayout.NORTH,jtf3,10,SpringLayout.SOUTH,jtf2);
        jtf3.setForeground(Color.black);
        jtf3.setFont(new Font(" ", 3, 15));

        //创建提交按钮
        JButton button=new JButton("提交");
        layout.putConstraint(SpringLayout.WEST,button,305,SpringLayout.WEST,panel);
        layout.putConstraint(SpringLayout.NORTH,button,300,SpringLayout.NORTH,panel);
        button.setForeground(Color.black);
        button.setFont(new Font(" ", 3, 15));
        JButton button1=new JButton("返回");
        layout.putConstraint(SpringLayout.WEST,button1,20,SpringLayout.EAST,button);
        layout.putConstraint(SpringLayout.NORTH,button1,300,SpringLayout.NORTH,panel);
        button1.setForeground(Color.black);
        button1.setFont(new Font(" ", 3, 15));

        JLabel img=new JLabel();
        ImageIcon imageIcon = new ImageIcon(".idea/83@8C1~2U@T_%X7@`C9A9]D_tmb.jpg");  // 将"path/to/your/image.jpg"替换为你的图片路径
        img.setIcon(imageIcon);
        //把所有控件添加至画布
        panel.add(label1);
        panel.add(jtf1);
        panel.add(label2);
        panel.add(jtf2);
        panel.add(label3);
        panel.add(jtf3);

        panel.add(button);
        panel.add(button1);
        panel.add(img);
        //把画布添加至面板
        frame.setContentPane(panel);
        //将面板中央显示
        new UIUtil().center(frame,frame_width,frame_height);
        //将面板显示
        frame.setVisible(true);


        //添加提交按钮的点击事件
        button.addMouseListener(new MouseListener() {
            @Override
            public void mouseClicked(MouseEvent e) {

            }

            @Override
            public void mousePressed(MouseEvent e) {
                //获得所有编辑框中添加的内容信息
                String personId=jtf1.getText();
                String name=jtf2.getText();
                String age=jtf3.getText();

                //把多个数据封装成一个对象，便于传输
                Map<String,Object> oMap=new HashMap<>();
                oMap.put("personId",personId);
                oMap.put("name",name);
                oMap.put("age",age);

                //把封装好的数据传递给业务逻辑层接口进行加工
                ICourseService service=  new CourseServiceImp();
                int i=service.addCourse(oMap);
                if (i>0){
                    //学生信息已添加数据库成功
//                    教师号必须存在
                    System.out.println("添加课程信息成功");
                    //隐藏面板
                    frame.setVisible(false);
                    //释放内存
                    frame.dispose();
                }

            }

            @Override
            public void mouseReleased(MouseEvent e) {

            }

            @Override
            public void mouseEntered(MouseEvent e) {

            }

            @Override
            public void mouseExited(MouseEvent e) {

            }
        });
        button1.addMouseListener(new MouseListener() {
            @Override
            public void mouseClicked(MouseEvent e) {

            }

            @Override
            public void mousePressed(MouseEvent e) {
                new TeacherGui();
                frame.dispose();
            }

            @Override
            public void mouseReleased(MouseEvent e) {

            }

            @Override
            public void mouseEntered(MouseEvent e) {

            }

            @Override
            public void mouseExited(MouseEvent e) {

            }
        });
    }

    public static void main(String[] args) {
        new AddCourseInfoUI();
    }
}
